﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using VietTrips.Domain.Contexts;
using VietTrips.Domain.Interfaces;
using VietTrips.Domain.Models;

namespace VietTrips.Domain.Repositories
{
    public class TourRepository : ITourRepository
    {
        readonly EntitiesContexts _contexts = new EntitiesContexts();

        
        public void Dispose()
        {
            if (_contexts != null)
            {
                _contexts.Dispose();
            }
        }

        public IQueryable<Tour> All { get { return _contexts.Tours.Where(to => !to.IsDeleted); } }
        public IQueryable<Tour> AllIncluding(params Expression<Func<Tour, object>>[] includeExpressions)
        {
            throw new NotImplementedException();
        }

        public Tour Find(int id)
        {
            return _contexts.Tours.Find(id);
            
        }

        public void Insert(Tour entity)
        {
            _contexts.Tours.Add(entity);
            this.Save();
        }

        public void Update(Tour entity)
        {
            _contexts.Entry(entity).State = EntityState.Modified;
            this.Save();
            
        }

        public void Delete(int id)
        {
            var tour = _contexts.Tours.Find(id);
            if (tour != null)
            {
                tour.IsDeleted = true;
                tour.LastModified = DateTime.Now;
                _contexts.Entry(tour).State = EntityState.Modified;
            }
            this.Save();
        }

        public void Save()
        {
            _contexts.SaveChanges();
        }
    }
}
